package com.czkj.hwz.decorate;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioGroup;

import com.czkj.hwz.decorate.fragment.FirstFragment;
import com.czkj.hwz.decorate.fragment.MessageFragment;
import com.czkj.hwz.decorate.fragment.MineFragment;
import com.czkj.hwz.decorate.fragment.ShopFragment;

import java.util.ArrayList;
import java.util.List;

public class FirstActivity2 extends AppCompatActivity {
    private List<Fragment> fragments=null;
    private ViewPager viewPager;
    private RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 21){
            Window window = getWindow();
            //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //设置状态栏颜色
            window.setStatusBarColor(getResources().getColor(R.color.yellow));
        }
        setContentView(R.layout.activity_first2);
        initDate();
        initView();
    }
    private void initDate(){
        fragments=new ArrayList<Fragment>();
        fragments.add(new FirstFragment());
        fragments.add(new MessageFragment());
        fragments.add(new ShopFragment());
        fragments.add(new MineFragment());
    }
    private void initView(){
        if(fragments==null){
            return;
        }
        viewPager=(ViewPager)findViewById(R.id.content_viewpager);
        radioGroup=(RadioGroup)findViewById(R.id.content_radiogroup);
        viewPager.setOffscreenPageLimit(3);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_shouye:
                        viewPager.setCurrentItem(0,false);
                        break;
                    case R.id.rb_msg:
                        viewPager.setCurrentItem(1,false);
                        break;
                    case R.id.rb_shop:
                        viewPager.setCurrentItem(2,false);
                        break;
                    case R.id.rb_mine:
                        viewPager.setCurrentItem(3,false);
                        break;
                }
            }
        });
        radioGroup.check(R.id.rb_shouye);
    }
}
